package com.my.swas.common.base;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @Description:
 * @author: :MaYong
 */
public class ResponseResult<T> implements Serializable {
    /**
     * 错误码
     */
    @Getter
    @Setter
    private String code;

    /**
     * 提示信息
     */
    @Getter
    @Setter
    private String msg;

    /**
     * 具体的内容
     */
    @Getter
    @Setter
    private T data;


    private ResponseResult(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data = data;
    }

    private ResponseResult(String msg) {
        this.code = ResultEnum.ERROR.getCode();
        this.data = null;
        this.msg = msg;
    }

    private ResponseResult(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    private ResponseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ResponseResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private ResponseResult(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }


    private static ResponseResult ok() {
        return new ResponseResult<>(ResultEnum.SUCCESS);
    }



    /**
     * 成功时候的调用
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(data);
    }

    /**
     * 成功时候的调用
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> ok(String msg,T data) {
        return new ResponseResult<>(msg,data);
    }

    /**
     * 根据返回的状态对象， 构建返回结果
     *
     * @param resultEnum
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> build(ResultEnum resultEnum) {
        return new ResponseResult<>(resultEnum);
    }

    /**
     * 根据 code， 和  msg 构建返回结果
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> build(String code, String msg) {
        return new ResponseResult<T>(code, msg);
    }

    /**
     * 根据 code， 和  msg, 和 data 构建返回结果
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> build(String code, String msg, T data) {
        return new ResponseResult<T>(code, msg, data);
    }


    private static ResponseResult error() {
        return new ResponseResult<>(ResultEnum.ERROR);
    }

    /**
     * 失败的调用
     *
     * @param codeMsg
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(String codeMsg) {
        return new ResponseResult<>(codeMsg);
    }

    /**
     * 失败的调用 将返回结果传入
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseResult<T> error(T data) {
        return new ResponseResult<>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), data);
    }


    public static ResponseResult ok = ok();

    public static ResponseResult error = error();
}
